﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.Domain;

namespace MShop.Core.DataAccess
{
    public class ShopUsersFavDao:CommonDao<ShopUsersFav>,IShopUsersFavDao
    {
        private readonly IProductDao _ProductDao;
        public ShopUsersFavDao(IObjectContextProvider provider,IProductDao productDao)
            : base(provider)
        {
            _ProductDao = productDao;
        }

        public virtual int UpdateFav(Guid userid, int favid)
        {
            int count = Find(x => x.FavType == "1" && x.FavId == favid && x.UserId == userid).Count();
            if (count == 0)
            {
                Product p = _ProductDao.GetById(favid);
                ShopUsersFav fav = new ShopUsersFav();
                fav.UserId = userid;
                fav.Created = DateTime.Now;
                fav.FavId = p.Id;
                fav.FavImageUrl = p.ImageUrl;
                fav.FavName = p.ProductName;
                fav.FavType = "1";
                Save(fav);
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}